﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace SMS.Common.Diagnostics
{
    public class SourceTracking
    {
        const string SOURCE_NAME = "TaurusGame";
        internal const int Error_UnhandledException = 2;
        private static TraceSource m_Tracking { get; set; }
        static SourceTracking()
        {
            m_Tracking = new TraceSource(SOURCE_NAME, SourceLevels.Off);
            //if (m_Tracking.Switch == null)
            //{
            //    m_Tracking.Switch = new SourceSwitch(SOURCE_NAME, SourceLevels.Off.ToString());
            //}
            //if (m_Tracking.Switch.Level!= SourceLevels.Off && m_Tracking.Listeners.Count == 0)
            //{
            //    string file = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "OnePlatform.log");
            //    TextWriterTraceListener listener = new TextWriterTraceListener(file);
            //    if (listener.TraceOutputOptions == TraceOptions.None)
            //    {
            //        listener.TraceOutputOptions = TraceOptions.DateTime;
            //    }
            //    else
            //    {
            //        listener.TraceOutputOptions |= TraceOptions.DateTime;
            //    }
            //    m_Tracking.Listeners.Add(listener);
            //}
        }

        public static void TraceInformation(string message, params object[] args)
        {
            m_Tracking.TraceInformation(message, args);
        }
        public static void TraceWarning(string message, params object[] args)
        {
            m_Tracking.TraceEvent(TraceEventType.Warning, 0, "[" + DateTime.Now + "]" + message, args);
        }
        public static void TraceEvent(TraceEventType eventType, int id, string message, params object[] args)
        {
            m_Tracking.TraceEvent(eventType, id, "[" + DateTime.Now + "]" + message, args);
        }
        public static void TraceError(string message, Exception ex)
        {
            if (!string.IsNullOrEmpty(message)) message += ":";
            m_Tracking.TraceEvent(TraceEventType.Error, 0, "[{0}]{1}{2}", DateTime.Now, message, ex);
        }
        public static void TraceError(Exception ex)
        {
            TraceError(null, ex);
        }
    }
}
